/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.mach.service.impl;

import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.mach.DrawingNodeMerger;
import org.springblade.mach.entity.DrawingInfo;

import org.springblade.mach.entity.DrawingInfoNode;
import org.springblade.mach.service.IDrawingInfoNodeService;
import org.springblade.mach.vo.DrawingInfoVO;
import org.springblade.mach.mapper.DrawingInfoMapper;
import org.springblade.mach.service.IDrawingInfoService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author LY
 * @since 2021-03-05
 */
@Service
public class DrawingInfoServiceImpl extends BaseServiceImpl<DrawingInfoMapper, DrawingInfo> implements IDrawingInfoService {
	private final static String PARENT_ID = "parentId";

	@Autowired
	private IDrawingInfoNodeService drawingInfoNodeService;


	@Override
	public List<DrawingInfoVO> getChileNodeByPId(Long pid) {
		return baseMapper.getChileNodeByPId(pid);
	}

	@Override
	public IPage<DrawingInfoVO> selectDrawingInfoPage(IPage<DrawingInfoVO> page, DrawingInfo drawingInfo) {
		return page.setRecords(baseMapper.selectDrawingInfoPage(page, drawingInfo));
	}

	@Override
	public List<DrawingInfoVO> lazyList(Long parentId, Map param) {
		if (Func.isEmpty(Func.toStr(param.get(PARENT_ID)))) {
			parentId = null;
		}
		return baseMapper.lazyList(parentId,param);
	}

	@Override
	public List<DrawingInfoVO> tree() {
		List<DrawingInfoVO> tree = baseMapper.tree();
		List<DrawingInfoVO> merge = DrawingNodeMerger.merge(tree);

		return merge;

	}

	@Override
	public List<DrawingInfoVO> getList(Map<String, Object> drawingInfo) {
		return baseMapper.getList(drawingInfo);
	}
	@Override
	public DrawingInfoVO getDrawingInfoByNodeId(Long nodeId) {
		return baseMapper.getDrawingInfoByNodeId(nodeId);
	}
	@Override
	public DrawingInfo getDrawingInfoByDrawingNumber(String drawingNumber) {
		return baseMapper.getDrawingInfoByDrawingNumber(drawingNumber);
	}

	@Override
	public List<DrawingInfoVO> lazyTree(Long parentCode,  Long id) {
		List<DrawingInfoVO> tree = baseMapper.lazyTree(parentCode, id);
		return DrawingNodeMerger.merge(tree);
	}

	@Override
	public List<DrawingInfoVO> getTreeList(DrawingInfo drawingInfo) {
		return baseMapper.getTreeList(drawingInfo);
	}

	@Override
	public DrawingInfo getDrawingInfo(String drawingNumber, String drawingName) {
		if (StringUtil.isEmpty(drawingNumber)) {
			throw new ServiceException("图纸代号不能为空");
		}
		if (StringUtil.isEmpty(drawingName)) {
			throw new ServiceException("图纸名称不能为空!");
		}
		return baseMapper.getDrawingInfo(drawingNumber, drawingName);
	}

	@Override
	public List<DrawingInfo> findParentDrawingInfoList(String subDrawingNumber) {
		if (StringUtil.isEmpty(subDrawingNumber)) {
			throw new ServiceException("图纸编码不能为空!");
		}
		List<DrawingInfo> drawingInfoList = new ArrayList<>();
		// 递归获取所有父级列表
		findRecursionDrawingInfoList(drawingInfoList, subDrawingNumber);
		return drawingInfoList;
	}

	@Override
	public List<DrawingInfo> findChildDrawingInfoList(Long parentId) {
		List<DrawingInfo> drawingInfoList = new ArrayList<>();
		findChildDrawingInfoList(drawingInfoList, parentId);
		return drawingInfoList;
	}

	/**
	 * 递归获取所有父级附件列表
	 */
	private void findRecursionDrawingInfoList(List<DrawingInfo> drawingInfoList, String subDrawingNumber) {
		List<Long> parentLevelIds = drawingInfoNodeService.findParentLevelIdList(subDrawingNumber);
		if (CollectionUtil.isNotEmpty(parentLevelIds)) {
			for (Long drawingInfoId : parentLevelIds) {
				DrawingInfo drawingInfo = this.getById(drawingInfoId);
				if (drawingInfo != null) {
					drawingInfoList.add(drawingInfo);
					findRecursionDrawingInfoList(drawingInfoList, drawingInfo.getDrawingNumber());
				}
			}
		}
	}

	/**
	 * 递归获取所有的子级节点
	 */
	public void findChildDrawingInfoList(List<DrawingInfo> drawingInfoList, Long uploadId){
		List<DrawingInfoNode> allChild = drawingInfoNodeService.findDrawingNodeList(uploadId);
		if (CollectionUtil.isNotEmpty(allChild)) {
			List<Long> childIdList = allChild.stream().map(DrawingInfoNode::getChildId).collect(Collectors.toList());
			for (Long childId : childIdList) {
				DrawingInfo drawingInfo = this.getById(childId);
				if (drawingInfo != null) {
					if (drawingInfo.getDrawingNumber().contains("MH")) {
						drawingInfoList.add(drawingInfo);
						findChildDrawingInfoList(drawingInfoList, drawingInfo.getId());
					}
				}
			}
		}
	}



}
